//
//  GphotoView.m
//  DitelHome
//
//  Created by 葛悦彣 on 2022/2/22.
//  Copyright © 2022 Ditel. All rights reserved.
//

#import "GphotoView.h"

@interface GphotoView()

@property (nonatomic ,strong) UIView * backview;
@property (nonatomic ,strong) UIImageView * imgview;
@property (nonatomic ,strong) UIImageView * deleteBtn;
@property (nonatomic ,strong) UILabel * countLab;
@property (nonatomic ,strong) UIImageView * bjimg;
@property (nonatomic ,strong) UIImageView * videoImg;

@end
@implementation GphotoView
+(instancetype)NewViewImg:(UIImage *)img andDeleteImg:(UIImage *)dimg {
    GphotoView * view = [GphotoView NewView];
    view.bjimg.hidden = NO;
    view.bjimg.image = img;
    view.deleteBtn.image = dimg;
    return view;
}
+(instancetype)NewViewImg:(UIImage *)img{
    GphotoView * view = [GphotoView NewView];
    view.bjimg.hidden = NO;
    view.bjimg.image = img;
    return view;
}

+(instancetype)NewView{
    
    GphotoView * view = [GphotoView new];
    
    __weak __typeof(view)weakview = view;
    UIView * backview = [UIView new].addClickBlock(^{
        if(weakview.addPhotoBlock){
            weakview.addPhotoBlock();
        }
   });
    backview.clipsToBounds = YES;
    view.backview = backview;
    [view addSubview:backview];
    [backview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.offset(0);
        make.width.height.offset(kwt(75));
    }];
    backview.backgroundColor = KcolorHex(@"#FBFBFB");
    [DTKTool YJ:kwt(5) andView:backview ];
    UIImageView * tempimg = kimageView(@"GBJL_addimg");
    [backview addSubview:tempimg];
    [tempimg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(kwt(10));
        make.centerX.offset(0);
        make.width.height.offset(kwt(15));
    }];
    
    UILabel * templab = [DTKTool labelWithText:@"上传凭证\n(最多8张)" andFont:kFont(13) andTextColor:KcolorHex(@"#9FA5AE")];
    view.countLab = templab;
    templab.textAlignment = NSTextAlignmentCenter;
    templab.numberOfLines = 2;
    [backview addSubview:templab];
    [templab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(kwt(34));
        make.centerX.offset(0);
    }];
    
    UIImageView * bjimg = [UIImageView new];
    view.bjimg = bjimg;
    bjimg.hidden = YES;
    [backview addSubview:bjimg];
    [bjimg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.offset(0);
        make.width.height.offset(kwt(75));
    }];
    
    
    UIImageView * imgview = [UIImageView new];
    imgview.hidden = YES;
    view.imgview = imgview;
    [backview addSubview:imgview];
    [imgview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.bottom.offset(0);
    }];
    imgview.userInteractionEnabled = YES;
    [imgview addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:view action:@selector(xzimg)]];
    UIImageView * deleteBtn = kimageView(@"GBJL_deleteimg");
    deleteBtn.hidden = YES;
    view.deleteBtn = deleteBtn;
    [view addSubview:deleteBtn];
    [deleteBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.right.offset(0);
        make.width.height.offset(kwt(20));
    }];
    deleteBtn.userInteractionEnabled = YES;
    [deleteBtn addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:view action:@selector(deleteBtnClick)]];
    
    UIImageView * videoImg = kimageView(@"maintenance_content_video");
    view.videoImg = videoImg;
    videoImg.hidden = YES;
    [backview addSubview:videoImg];
    [videoImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(kwt(25));
        make.bottom.offset(kwt(-25));
        make.width.height.offset(kwt(20));
    }];
    
    
    return view;
}
- (void)setCount:(int)count {
    self.countLab.text = [NSString stringWithFormat:@"上传凭证\n(最多%d张)",count];
}
- (void)setMsStr:(NSString *)msStr{
    self.bjimg.hidden = YES;

    self.countLab.text = msStr;
}
- (void)setPhotoUrl:(NSString *)PhotoUrl {
    self.videoImg.hidden = YES;
    [self.imgview sd_setImageWithURL:[NSURL URLWithString:PhotoUrl]];
    self.deleteBtn.hidden = NO;
    self.imgview.hidden = NO;
}
- (void)setImg:(UIImage *)img {
    [self.imgview setImage:img];
    self.imgview.hidden = NO;
    self.deleteBtn.hidden = NO;
//    self.bjimg.hidden = YES;
}
- (void)setBjImg:(UIImage *)bjImg {
    self.bjimg.image = bjImg;

}
- (void)xzimg{
    if(self.choosePhotoBlock){
        self.choosePhotoBlock();
    }
}
- (void)deleteBtnClick{
    if(self.deletePhotoBlock){
        self.deletePhotoBlock();
    }
}
- (void)newHideen {
    self.hidden = NO;
    self.imgview.hidden = YES;
    self.deleteBtn.hidden = YES;
    self.videoImg.hidden = YES;
//    self.bjimg.hidden = YES;

}
- (void)setIsVideo:(BOOL)isVideo{
    self.videoImg.hidden = !isVideo;
}
@end
